﻿using System.Text;

namespace Communications.PropertySearch
{
    public sealed class PropertyCriteria
    {
        public bool WithImages { get; set; }
        public string Keywords { get; set; }
        public string KeywordsExclude { get; set; }
        public string PlaceName { get; set; }
        public PropertyType PropertyType { get; set; }
        public ListingType ListingType { get; set; }

        public int? MinimumBedrooms { get; set; }
        public int? MaximumBedrooms { get; set; }
        public int? MinimumBathrooms { get; set; }
        public int? MaximumBathrooms { get; set; }
        public int? MinimumRooms { get; set; }
        public int? MaximumRooms { get; set; }
        public int? MinimumPrice { get; set; }
        public int? MaximumPrice { get; set; }

        public string SerializeToString()
        {
            var sb = new StringBuilder();

            sb.Append("&withimage_only=" + (WithImages ? "1" : "0"));
            sb.Append("&keywords=" + Keywords);
            sb.Append("&keywords_exclude=" + KeywordsExclude);
            sb.Append("&place_name=" + PlaceName);
            sb.Append("&property_type=" + PropertyTypeAsString());
            sb.Append("&listing_type=" + ListingTypeAsString());

            sb.Append("&bedroom_min=" + MinimumBedrooms ?? "min");
            sb.Append("&bedroom_max=" + MaximumBedrooms ?? "max");
            sb.Append("&bathroom_min=" + MinimumBathrooms ?? "min");
            sb.Append("&bathroom_max=" + MaximumBathrooms ?? "min");
            sb.Append("&room_min=" + MinimumRooms ?? "min");
            sb.Append("&room_max=" + MaximumRooms ?? "max");
            sb.Append("&price_min=" + MinimumPrice ?? "min");
            sb.Append("&price_max=" + MaximumPrice ?? "max");

            return sb.ToString();
        }

        private string PropertyTypeAsString()
        {
            return PropertyType == PropertyType.Undefined ? null : PropertyType.ToString().ToLowerInvariant();
        }

        private string ListingTypeAsString()
        {
            return ListingType == ListingType.Undefined ? null : ListingType.ToString().ToLowerInvariant();
        }
    }
}